/**
 * Created by Administrator on 2017/5/4 0004.
 */

checkCookie();
var mid = $.cookie('mid')
var token = $.cookie('token')
//循环日期
// var mydate = new Date();
// var month = mydate.getMonth() + 1;
// var date = mydate.getDate() + 1;
// var week = mydate.getDay() + 1;
// var arr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六',
//     '周日', '周一', '周二', '周三', '周四', '周五', '周六',
//     '周日', '周一', '周二', '周三', '周四', '周五', '周六'];
//for(var i=0;i<7;i++){
//    $('.date1 li').eq(i).find('.week').html(arr[week+i]);
//    $('.date1 li').eq(i).find('.month_date').html(month+'-'+(date+i))
//}
//$('.date1 li').find('.week').eq(0).html('明天');
//$('.date1 li').find('.month_date').eq(0).html(month+'-'+date);
//截取参数
var searchURL = window.location.search;
searchURL = searchURL.substring(1, searchURL.length);
var d_id = searchURL.split("&")[0].split("=")[1];
localStorage.doc_id = d_id;
if (searchURL.indexOf('day') != -1) {
    var day = searchURL.split("&")[1].split("=")[1];
}
//懒加载
var next_page = '';
var data_back = false;
$(window).scroll(function () {
    var windowheight = $(window).height();//窗口高度
    var scrolltop = $('body').scrollTop();//滚动条高度
    var eh = $(document).height();//文本高度
    if (parseInt(windowheight + scrolltop) >= eh) {
        $('.bot_loading').show()
        if (data_back) {
            return false
        }
        data_back = true;
        if (next_page == null) {
            $('.bot_loading').html('没有更多啦！');
            setInterval(function () {
                $('.bot_loading').fadeOut(800)
            }, 2000);
            data_back = false
        } else {
            getData({url: next_page + '&id=' + d_id, data: {mid: mid, token: token}}, function (data) {
                var html = template('comment_list', data);
                $('.comment_list').append(html)
                $('.bot_loading').hide();
                next_page = data.comment.data.next_page_url
                data_back = false
            })

        }

    } else {
        $('.bot_loading').hide()
    }
})
//请求数据
getData({url: doctor_info, data: {mid: mid, token: token, id: d_id}}, function (data) {
    var html = template('doctor', data);
    $('#main').html(html);
    var htnlone = template('yuyue', data)
    $('#all .box_top').html(htnlone)
    $('.load').hide()
    var childs = $('.comment_list').find('li')
    if (childs.length == 0) {
        $('.comment').append('<p style="text-align: center;padding-top: 10px; line-height: 60px;">暂无评论</p>')
    }
    next_page = data.comment.data.next_page_url;
    //点击关注/取消关注
    $('#concern').on('touchstart', function () {
        var self = $(this);
        var id = self.attr('d_id');
        var cls = self.attr('class');
        if (cls == 'guanzhu') {
            getData({url: follow, type: 'post', data: {mid: mid, token: token, d_id: d_id}}, function (res) {
                if (res.state == 1) {
                    self.attr('class', 'yiguanzhu').attr('d_id', d_id).html('已关注');
                }
            });
        } else {
            getData({url: cancle_follow, data: {mid: mid, token: token, id: id}}, function (res) {
                if (res.state == 1) {
                    self.attr('class', 'guanzhu').html('关注');
                }
            });
        }

    });
    //点击咨询
    $('.consultation').on('touchstart', function () {

    })
    //点击预约
    $('.yuyue').on('touchstart', function () {
        $('#all').show();
        $('body').css('position', 'fixed');
        // $('.box_top').html('')
        $('.choose').html('');
        $('.date1_list').html('')
        // for (var i = 0; i < 7; i++) {
        //     $('.date1_list').append(' <li class="date_active"><div><p class="week"></p><p class="month_date"></p></div></li>')
        //     $('.date1 li').eq(i).find('.week').html(arr[week + i]);
        //     $('.date1 li').eq(i).find('.month_date').html(month + '-' + (date + i))
        // }

        var doc_id = $(this).attr('d_id');
        //请求数据
        getData({url: yuyue, data: {mid: mid, token: token, id: doc_id}}, function (data) {
            // var html=template('yuyue',data);
            // $('.box_top').html(html);
            var htmlone = template('choose_date', data);
            $('.choose_time ul').html(htmlone);
            var html=template('date',data)
            $('.date1_list').html(html)
            var childs = $('.choose').find('li')
            if (childs.length == 0) {
                $('.choose').append('<p style="text-align: center;padding-top: 10px;font-size:15px; color: #666;">暂无数据</p>')
            }
            $('.date1 li').find('.week').eq(0).html('明天');
            $('.date1_list li').find('div').removeClass('tag')
            $('.date1_list li').eq(0).find('div').addClass('tag')
            $('.date_active').on('touchstart', function () {
                $(this).siblings().find('div').removeClass('tag')
                $(this).find('div').addClass('tag')
            });
            //点击日期发起请求
            $('.date1_list li').on('touchstart', function () {
                //  $('.choose_time').find('p').eq(1).html('')
                $('.choose_time ul').html('');
                var key1 = $(this).index() + 1;
                $('.date1_list li').css('pointer-events', 'none');
                localStorage.date = key1;
                getData({url: yuyue, data: {mid: mid, token: token, id: doc_id, key: key1}}, function (data) {
                    var htmlone = template('choose_date', data);
                    $('.choose_time ul').html(htmlone);
                    $('.date1_list li').css('pointer-events', 'auto');
                    var childs = $('.choose_time ul').find('li')
                    if (childs.length == 0) {
                        $('.choose_time ul').append('<p style="text-align: center;padding-top: 0px;font-size:15px; color: #666;">暂无数据</p>')
                    }

                    $('.yy').on('touchstart', function () {
                        var key2 = $('#all .tag').parent().index() + 1;
                        // localStorage.work_time=$(this).attr('work_time')//localStorage预约时间
                        var time = $(this).attr('work_time')
                        localStorage.work_time = time;
                        window.location.href = 'finish-reference.html?d_id=' + doc_id + '&time=' + time + '&key=' + key2;
                    })
                });
            })
            $('.yy').on('touchstart', function () {
                var key2 = $('#all .tag').parent().index() + 1;
                // localStorage.work_time=$(this).attr('work_time')//localStorage预约时间
                var time = $(this).attr('work_time')
                window.location.href = 'finish-reference.html?d_id=' + doc_id + '&time=' + time + '&key=' + key2;
            })

            var myScroll = new iScroll('wrapper', {
                vScroll: false,
                hScroll: true,
                hScrollbar: false
            })
            //tab切换日期
            $('.date_active').on('touchstart', function () {
                $(this).siblings().find('div').removeClass('tag');
                $(this).find('div').addClass('tag');

            })

        })
    })

});
//点击关闭
$('.close').on('touchstart', function () {
    $('#all').hide();
    $('body').css('position', '');
    $('.comment_list li').css('pointer-events', 'none');
    setTimeout(function () {
        $('.comment_list li').css('pointer-events', 'auto');
    }, 400)
});

